home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / maxima-5.9.0.exe / {app} / share / maxima / 5.9.0 / src / commac.lisp < prev    next >
Encoding:
Text File  |  2003-02-09  |  35.0 KB  |  1,167 lines

  1. ;; -*-  Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;;                                                                    ;;;;;
  4. ;;;  Copyright (c) 1984,1987 by William Schelter,University of Texas   ;;;;;
  5. ;;;     All rights reserved                                            ;;;;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7. (in-package "MAXIMA")
  8. (eval-when (compile)
  9. (proclaim '(optimize (safety 0) (speed 3) (space 0)))
  10. )
  11.  
  12. (eval-when (compile load eval)
  13.  
  14. (defmacro deffif (new old)
  15.   (assert (and  (symbolp new) (symbolp old)))
  16.   (cond ((eql new old) nil)
  17.     (t `(deff ,new (function ,old)))))
  18.  
  19. (defmacro def-copy-special (fn symbol-to-copy)
  20.   (cond ((not (eql fn symbol-to-copy))
  21.      `(defmacro ,fn (&rest l)
  22.         `(,',symbol-to-copy ,@  l)))))
  23.  
  24. (defmacro defun-prop (f arg &body body)
  25.   (assert (listp f))
  26.   #+lispm
  27.   `(defun (:property ,@ f) ,arg ,@ body)
  28.   #+gcl
  29.   (eval-when (eval )
  30.          (compiler::compiler-def-hook (car f) body))
  31.   #-lispm
  32.   `(progn 'compile
  33.       (setf (get ',(car f) ',(second f))
  34.     #'(lambda ,arg
  35.         ,@ body))))
  36.  
  37. (progn 'compile
  38. ;(deffif zl-REM  global:REM) ;;had hokey definition in macsyma remprop--I replaced by remprop
  39. )
  40.  
  41. #+lispm
  42.  
  43. (deff %data-type  #'si::%data-type)
  44.  
  45. #+(or symbolics tirel3) 
  46. (eval-when (compile load)
  47. (deff $make_hash_table #'make-hash-table)
  48. (deff ATAN  #'global:ATAN) ;shadow it .. wrong def
  49. (deff ATAN2 #'global:ATAN2)
  50. (deff ERRSET #'global:ERRSET)
  51. (deff font-char-height #-ti #'global:font-char-height #+ti #'tv:font-char-height)
  52. (deff font-char-width #-ti #'global:font-char-width #+ti #'tv:font-char-width )
  53.  
  54. )
  55. )
  56.  
  57. (defvar prin1 nil) ;a function called instead of prin1.
  58.  
  59. (eval-when (load compile eval)
  60. (defvar *allow-redefines* t)
  61. )
  62.  
  63. ;(defmacro defun-if-new (fun args &body body)
  64. ;  `(cond ((and (null *allow-redefines*) (fboundp ',fun)) nil)
  65. ;    (t (progn 'compile
  66. ;           (si::record-source-file-name ',fun 'defun-if-new)
  67. ;           (defun ,fun ,args
  68. ;              ,@ body)))))
  69.  
  70. (eval-when (load compile eval)
  71. (defun appears (tree var) (cond ((equal tree var)  (throw 'appears t))
  72.                ((atom tree) nil)
  73.                (t  (appears  (car tree) var) (appears (cdr
  74.        tree)  var))) nil)
  75.  
  76. (defun appears1 (tree var)
  77.     (cond ((eq tree var)
  78.            (throw 'appears t))
  79.           ((atom tree) nil)
  80.           (t
  81.            (appears (car tree) var)
  82.            (appears (cdr tree) var)))
  83.     nil)
  84.  
  85. (defun appears-in (tree var)
  86.     "Yields t if var appears in tree"
  87.     (catch 'appears
  88.             (cond ((or (symbolp var)
  89.                        (fixnump var))
  90.                    (appears1 tree var))
  91.                   (t (appears tree var)))))
  92.  )
  93.  
  94.  
  95.  
  96. ;;this wants the input type to be eg.  'fixnum  and outputs the same 'fixnum
  97. ;;eg (maclisp-typep 5) ==> 'fixnum
  98. ;;eg (maclisp-typep 6 'fixnum) ==> t
  99.  
  100. ;;;this is much faster but depends on the %data-type function
  101. ;;;Actually the optimizer should eliminate any calls in compiled code
  102. ;;;to the two argument ml-typep.  And these should be eliminated anyway
  103. ;;;since they were not part of maclisp.  Ultimately we should 
  104. ;;;make the optimizer branch to a one arg typep (maclisp-type-of) 
  105. ;;;when there is only one argument.
  106.  
  107.  
  108. ;(defvar *primitive-data-type-function* 'type-of)
  109. ;it is faster to use this on the lispm, but type-of would be ok.
  110. ;in kcl one would use the number code given by type_of(x);
  111.  
  112. (eval-when (compile load)
  113. #+lispm
  114. (defvar *primitive-data-type-function* 'si::%data-type)
  115. #-lispm
  116. (defvar *primitive-data-type-function* 'type-of)
  117. )
  118. (defmacro one-of-types (typ &rest objs &aux all)
  119.   "typ is a primitive data type of the machine, and"
  120.   (dolist (v objs
  121.          (cond ((cdr all)`(memq ,typ ',all))
  122.             (t `(eql ,typ ',(car all)))))
  123.     (pushnew  (funcall *primitive-data-type-function* (eval v)) all)))
  124.  
  125. (defun maclisp-typep (x &optional type)
  126.   (cond (type
  127.      (lisp:let (( pred (get type 'ml-typep)))
  128.         (cond (pred
  129.         (funcall pred x))
  130.            (t (typep x type)))))
  131.     (t
  132.     (lisp:let ((.type. (#. (if (boundp '*primitive-data-type-function*)
  133.                    *primitive-data-type-function* 'type-of)
  134.                    x)))
  135.       (cond
  136.        ((one-of-types .type.  'hi nil) 'symbol)
  137.        ((one-of-types .type. '(a)) 'list)
  138.        ((one-of-types .type. 3) 'fixnum)
  139.        ((one-of-types .type.  (make-array 3) "abc")
  140.         (cond ((stringp x) 'string) ;;should really be symbol 'ugggh
  141.           #+ti ((hash-table-p x) 'hash-table)
  142.           (t 'array)))
  143.        ((one-of-types .type. (expt 2 50) 1.234 most-positive-single-float
  144.               most-positive-double-float most-positive-long-float )
  145.           (cond 
  146.             ((integerp x) 'bignum)
  147.             ((floatp x) 'flonum )
  148.             (t 'number)))
  149.        ;;note the following is 'random in maclisp
  150.        ((one-of-types .type. #'cons) 'compiled-function)
  151.        #-ti ((one-of-types .type.  (make-hash-table))
  152.         (cond ((hash-table-p x) 'hash-table)
  153.           (t (type-of x))))
  154.        ((arrayp x) 'array)
  155.        ;((one-of-types .type. (make-array '(2 3)))  'array)
  156.        (t (type-of x)))))))
  157.  
  158.  
  159. (deff ml-typep #'maclisp-typep)
  160. ;;so that (ml-typep a 'list) ==> (zl-listp a)
  161.  
  162. #+symbolics
  163. (progn
  164. compiler:
  165. (defoptimizer (ml-typep maxima-typep-two-args) (form)
  166.   (cond ((and (= (length form) 3)
  167.           (constant-form-p (third form)))    ;constant type
  168.      (lisp:let* ((typed-form (second form))
  169.         (type (constant-evaluator (third form)))
  170.         (pred (and (symbolp type)
  171.                (or (get type 'ml-typep)))))
  172.        (cond (pred `(,pred ,typed-form))
  173.          (t
  174.           (cons 'global:typep (cdr form))))))
  175.     (t form)))
  176.  
  177. )
  178.  
  179. (defprop :extended-number extended-number-p ml-typep)
  180. (defprop array arrayp ml-typep)
  181. (defprop atom  atom ml-typep)
  182.  
  183. #+cmu (shadow '(lisp::compiled-function-p) (find-package "MAXIMA"))
  184. #+cmu (defun compiled-function-p (x)
  185.     (and (functionp x) (not (symbolp x))
  186.          (not (eval:interpreted-function-p x))))
  187.  
  188. (defprop compiled-function compiled-function-p ml-typep)
  189. (defprop extended-number extended-number-p ml-typep)
  190. (defprop fixnum fixnump ml-typep)
  191. (defprop list consp ml-typep)
  192. (defprop number numberp ml-typep)
  193. (defprop string stringp ml-typep)
  194. (defprop symbol  symbolp ml-typep)
  195.  
  196. (defvar *make-array-option-replacements*
  197.     '((:element-type  :type
  198.                ( t . art-q)
  199.                ( string-char . art-string))
  200.       (:initial-element  :initial-value nil)
  201.       (:adjustable                ;remove :adjustable
  202.        )))                    
  203.  
  204. (defvar *cl-make-array-option-replacements*
  205.     '((:type  :element-type
  206.                (  art-q  . t)
  207.                ( art-1b  . (mod 2))
  208.                (art-8b   . (mod 8))
  209.                (art-4b   . (mod 4))
  210.                ( art-string . character  ))
  211.       (:initial-value  :initial-element nil)
  212.       (:adjustable                ;remove :adjustable
  213.        )))                    
  214.  
  215.  
  216. (defun maxima-copy-rest (form)
  217.   "copy those things out of the stack in `(($hi array) ,@ inds) which would be bad if inds is rest arg"
  218.   (copy-list form))
  219.  
  220.  
  221. (defun substitute-keyword-arg ( repl keyword-rest-arg &aux tem answ)
  222.   (sloop for (key arg) on keyword-rest-arg by 'cddr
  223.     do 
  224.     (cond ((setq tem (zl-ASSOC key repl))
  225.            (cond ((cdr tem)
  226.               (push (sublis (cddr tem) arg) answ)
  227.               (push (second tem) answ))))
  228.           (t (push arg answ) (push key answ))))
  229.   answ)
  230.  
  231.  
  232. (defun zl-make-array (dimensions &rest options)
  233.  (apply `make-array dimensions   (substitute-keyword-arg *cl-make-array-option-replacements* options)))
  234.  
  235. ;;I finally decided to change the typep.  It can be speeded up later.
  236. ;;The compiler can optimize (ml-typep 5 'fixnum) to (fixnump 5) etc.
  237. ;;if we give it the right properties.
  238.  
  239.  
  240.   
  241. ;;;note *array takes 'fixnum and 'flonum as its keyword args!!!!
  242. ;;need to use our selectq to ensure the type is correct
  243. ;(ARRAY CONUNMRK NIL (f1+ CONNUMBER))
  244.  
  245. (defvar *maxima-arrays* nil "Trying to track down any functional arrays in maxima")
  246.  
  247. ;;only remaining calls are for maclisp-type = nil
  248. (DEFUN *ARRAY (NAME MACLISP-TYPE &REST DIMLIST &AUX AARRAY)
  249.   (cond ((MEMQ MACLISP-TYPE '(READTABLE OBARRAY))
  250.      (ERROR " bad type ~S" MACLISP-TYPE)))
  251.   (pushnew name *maxima-arrays*) ;for tracking down old ones.
  252.   (SETQ AARRAY (make-array DIMLIST   ':INITIAL-element
  253.                (case MACLISP-TYPE 
  254.                  (FIXNUM 0)
  255.                  (FLONUM 0.0)
  256.                  (OTHERWISE NIL))))
  257.   (COND ((NULL NAME)
  258.      AARRAY)
  259.     ((SYMBOLP NAME)
  260.      (setf (symbol-array NAME) AARRAY)
  261.      NAME)
  262.     (T (error "~S is illegal first arg for *ARRAY" NAME))))
  263.  
  264. (DEFMACRO ARRAY (NAME MACLISP-TYPE &REST DIMLIST)
  265.   `(*ARRAY ',NAME ',MACLISP-TYPE ,@ DIMLIST))
  266.  
  267. ;;;    Change maclisp array referencing.
  268. ;;;   Idea1: Make changes in the code which will allow the code to still run in maclisp,
  269. ;;;yet will allow, with the appropriate macro definitions of array,arraycall, etc,
  270. ;;;to put the array into the value-cell.
  271. ;;;   Idea2: Make changes in the array referencing of (a dim1 dim2..) to (arraycall nil (symbol-array a) dim1..)
  272. ;;;which would then allow expansion into something which is common lisp compatible, for
  273. ;;;the day when (a 2 3) no longer is equivalent to (aref (symbol-function a) 2 3).
  274. ;;;I.  change (array a typ dim1 dim2..) to expand to (defvar a (make-array (list dim1 dim2 ...) :type typ')
  275. ;;;II. change (a dim1 dim2..) to (arraycall nil (symbol-array a) dim1 dim2 ..)
  276. ;;;III define 
  277. ;(defmacro symbol-array (ar)
  278. ;    `(symbol-function ,ar))
  279. ;(defmacro arraycall (ignore ar &rest dims)
  280. ;  `(aref ,ar ,@ dims))
  281. ;;;IV. change array setting to use (setf (arraycall nil ar dim1.. ) val) 
  282. ;;;which will generate the correct setting code on the lispm and will
  283. ;;;still work in maclisp.
  284.  
  285. (defmacro MAXIMA-ERROR (ctl-string &rest args)
  286.   `(cerror "without any special action" ,ctl-string ,@ args))
  287.  
  288. (defmacro safe-value (sym)
  289.   (cond ((symbolp sym)
  290.      `(cond ((symbolp ',sym)
  291.          (and (boundp ',sym) ,sym))
  292.         (t ,sym)))
  293.     (t nil)))
  294.  
  295. #-lispm
  296. (defmacro with-polynomial-area (ign &body body) ign
  297.   `(progn ,@ body))
  298.  
  299. (defmacro show (&rest l)
  300.   (sloop for v in l
  301.     collecting `(format t "~%The value of ~A is ~A" ',v ,v) into tem
  302.     finally (return `(progn ,@ tem))))
  303.  
  304. (defmacro defquote  (fn (aa . oth) &body rest &aux help ans ) 
  305.   (setq help (intern (format nil "~a-aux" fn)))
  306.   (cond ((eq aa '&rest)
  307.      (setq ans
  308.            (list
  309.         `(defmacro ,fn ( &rest ,(car oth) )
  310.            `(,',help  ',,(car oth)))
  311.         `(defun ,help (,(car oth)) . ,rest))))
  312.     (t (cond ((member '&rest oth)
  313.           (error "at present &rest may only occur as first item in a defquote argument")))
  314.        (setq ans
  315.          (list
  316.           `(defmacro ,fn (,aa . other  )
  317.              (setq other (sloop for v in other collecting (list 'quote v)))
  318.              (check-arg other (eql (length other) ,(length oth)) ,(format nil "wrong number of args to ~a" fn))
  319.              `(,',help  ',,aa   ,@ other))
  320.           `(defun ,help (,aa ,@ oth) . ,rest)))))
  321.   `(progn 'compile . , ans))
  322.  
  323.  
  324. (defquote $mdefvar (&rest l)
  325.   `((defvar) ,@ l))
  326.  
  327. ;;the resulting function will translate to defvar and will behave
  328. ;;correctly for the evaluator.
  329.  
  330. ;(defun gg fexpr (ll)
  331. ;       body)
  332. ;(defquote gg (&rest ll)
  333. ;       body)
  334.  
  335. ;(DEFQUOTE GG ( &rest C)
  336. ; (list  (car c) (second c) ))
  337. ;;the big advantage of using the following over defmspec is that it 
  338. ;;seems to translate more easily, since it is a fn.
  339. ;;New functions which wanted quoted arguments should be defined using
  340. ;;defquote
  341.  
  342.  
  343. (defun onep (x) (eql 1 x))
  344.  
  345. (defun extended-number-p (x)
  346.   #+lispm
  347.   (eql (%data-type x)  (%data-type 10000000000000000))
  348.   ;si:dtp-extended-number
  349.   #-lispm
  350.   (member (type-of x) '(bignum rational float ))
  351.   )
  352.  
  353. ;(defun si::extendp (x) (and (numberp x) (not (fixnump x))))
  354.  
  355. (defvar *scan-string-buffer*  nil)
  356.  
  357. #+lispm 
  358. (progn 
  359. ;;stuff for temporary area collections and copying.
  360.  
  361. (deffif STORE-ARRAY-LEADER global:STORE-ARRAY-LEADER)
  362. (deff %AREA-NUMBER #'si::%AREA-NUMBER)
  363. (defmacro copy-number (x)
  364.   (cond ((fboundp 'si:copy-extended-number) `(si:copy-extended-number ,x))
  365.     (t `(f- (f- ,x)))))
  366.  
  367.  
  368.  
  369. (defvar *temporary-polynomial-area*)
  370.  
  371. ;;fixed to handle (cons 1 bad-bignum)
  372. (defun copy-atomic-structure
  373.   (atomic-object
  374.    &optional (area-to-avoid *temporary-polynomial-area*) )
  375.   (select (%data-type atomic-object)
  376.       (si:dtp-extended-number
  377.        (cond ((eql (%area-number atomic-object) area-to-avoid )
  378.           (copy-number atomic-object ))
  379.          (t atomic-object)))
  380.       (#-ti si:dtp-array #+ti si:dtp-array-pointer
  381.         (cond ((eql (%area-number atomic-object) area-to-avoid)
  382.                (lisp:let
  383.             (ar array-options)
  384.             (cond ((named-structure-p atomic-object)
  385.                    (setq array-options (list
  386.                             :named-structure-symbol
  387.                             (aref atomic-object 0)))))
  388.                    (setq array-options
  389.                      (nconc array-options
  390.                         (list
  391.                          :type (array-element-type atomic-object)
  392.                          :leader-length (array-leader-length atomic-object))))
  393.                    (setq ar (apply 'global:make-array (array-dimensions atomic-object)
  394.                            array-options))
  395.                    (cond ((eq (array-type ar) 'art-q)
  396.                       (lisp:let ((from atomic-object) (to ar)        
  397.                                (to-length (length ar))
  398.                                (index 0))
  399.                               #-ti (declare (si:array-register from to))
  400.                               (sloop for ind from index below to-length
  401.                                 do (aset (copy-from-temporary-area
  402.                                       (aref from ind)) to ind))
  403.                               (sloop for i from 0 below  (or (array-leader-length from) 0)
  404.                                 do (store-array-leader
  405.                                 (copy-from-temporary-area
  406.                                  (array-leader from i)) TO I))))
  407.                      (t (copy-array-contents-and-leader atomic-object ar)))
  408.                    ar))
  409.               (t atomic-object)))
  410. ;;;        ((and (typep tree :symbol) (check-tree-for-area (symbol-plist tree))) tree)
  411.       (t atomic-object)))
  412.  
  413. (DEFUN copy-from-temporary-area (TREE &OPTIONAL
  414.                  (area-to-avoid *temporary-polynomial-area*)
  415.                  &aux tem)
  416.   "If the first cons is in area to avoid it copys whole list and cdr codes,
  417.   but if not it goes to the next cons and repeats."
  418.   (IF (ATOM TREE)
  419.       (cond ((or (fixnump tree) (symbolp tree)) tree)
  420.         (t(copy-atomic-structure tree area-to-avoid)))
  421.       (lisp:let ((RESULT (cond ((eql (%area-number tree) area-to-avoid )(copy-list  tree))
  422.               (t  TREE))))
  423.     (cond ((eq result tree)
  424.            (DO ((R RESULT (CDR R)))((atom r) result)
  425.          (COND ((EQL (%AREA-NUMBER (CDR r) ) AREA-TO-AVOID)
  426.             ;;some of the later cons's in tree might be in bad area
  427.             (setf (cdr R) (copy-from-temporary-area   (cdr R) area-to-avoid))))
  428.          (RPLACA R (copy-from-temporary-area (CAR R)))))
  429.           (t
  430.            (DO ((R RESULT (CDR R)))
  431.            ((atom r) result)
  432.          (COND ((AND (ATOM (CDR r))
  433.                  (EQL (%AREA-NUMBER (CDR r) ) AREA-TO-AVOID))
  434.             (setf (cdr  R) (copy-atomic-structure  (cdr R) area-to-avoid))))
  435.          (setq tem (car r))
  436.          (cond ((or (fixnump tem) (symbolp tem)))
  437.                (t 
  438.             (RPLACA R (copy-from-temporary-area
  439.                     tem area-to-avoid))))))))))
  440. ;;end of lispm stuff for areas.
  441. )
  442.  
  443. (defun macsyma-read-string (a-string &aux answer)
  444.   (cond ((not  (or (string-search "$" a-string) (string-search ";" a-string)))
  445.      (vector-push-extend #\$ a-string)))
  446.   (with-input-from-string (stream a-string)
  447.               (setq answer (third (mread stream)))
  448.               answer))
  449.  
  450. (defvar *sharp-read-buffer*
  451.   (make-array 140 :element-type ' #.(array-element-type "abc") :fill-pointer 0
  452.           :adjustable t))
  453.  
  454. (defun x$-cl-macro-read (&optional  stream arg &rest rest-arg)
  455.   rest-arg ;ignored
  456.   ($-read-aux arg stream))
  457.  
  458. (defun $-read-aux (arg stream &aux (meval-flag t) (*mread-prompt* ""))
  459.   (declare (special *mread-prompt*))
  460.   arg                    ;ignore
  461.   (setf (fill-pointer *sharp-read-buffer* ) 0)
  462.   (cond ((eql #\$ (tyipeek t stream))(tyi stream)
  463.      (setq meval-flag nil)))
  464.   (with-output-to-string
  465.    (st *sharp-read-buffer*) 
  466.    (let (char)
  467.      (sloop while (not (eql char #\$))
  468.        do
  469.        (setq char (tyi stream))
  470.        (tyo char st)
  471.        finally (cond ((not (eql  char #\$))
  472.               (error "There was no matching $" ))))))
  473.   (cond (meval-flag 
  474.      (list 'meval* (list 'quote
  475.                  (macsyma-read-string *sharp-read-buffer*))))
  476.     (t  (list 'quote  (macsyma-read-string *sharp-read-buffer*)))))
  477.  
  478.  
  479. (set-dispatch-macro-character  #\#  #\$ 'x$-cl-macro-read)
  480. (defvar *macsyma-readtable*)
  481.  
  482. (defun find-lisp-readtable-for-macsyma ()
  483.   (cond ((and (boundp '*macsyma-readtable*)
  484.           (readtablep *macsyma-readtable*))
  485.      *macsyma-readtable*)
  486.     (t (setq *macsyma-readtable* (copy-readtable nil))
  487.       (set-dispatch-macro-character
  488.        #\# #\$ 'x$-cl-macro-read *macsyma-readtable*)
  489.        *macsyma-readtable*)))
  490.  
  491. (defun set-readtable-for-macsyma ()
  492.   (setq *readtable* (find-lisp-readtable-for-macsyma)))
  493.  
  494.  
  495.  
  496.  
  497. #+ti ;;till its fixed by ti.
  498.  
  499. (defun signum (x) (cond ((> x 0) +1)
  500.             ((< x 0) -1)
  501.             ((zerop x) 0)
  502.             (t (error "bad arg to signum"))))
  503.  
  504.  
  505. ;;;to handle the maclisp (defun foo narg .. syntax.)
  506. ;;;see below for simpler method, but we have to redefine arg etc.
  507. ;;need to 
  508. ;;  I. convert to (defun foo (&rest narg-rest-argument (&aux (narg (length narg-rest-argument)))
  509. ;;  II. replace (arg 1) by (nth-arg 1)  and (listify i)  by  (narg-listify i) using the following definitions.
  510. ;;probably better not to shadow the listify etc. since someone might try to compile some maclisp code
  511. ;;and we should not break that.
  512. ;;REPLACEMENTS:
  513. ;'(("arg" . "narg-arg")
  514. ;  ("listify" . "narg-listify")
  515. ;  ("setarg" . "narg-setarg"))
  516. ;;new macros:
  517. (defun lastn (n x)
  518.    (nthcdr (f- (length x) n) x))
  519.  
  520. (defmacro narg-arg (x)
  521.   `(nth (f1- ,x) narg-rest-argument ))
  522. (defun narg-listify1 (x list)
  523.   (cond ((minusp x) (lastn (abs x) list))
  524.     (t (firstn x list))))
  525. (defmacro narg-listify (x)
  526.    `(narg-listify1 ,x narg-rest-argument))
  527. (defmacro narg-setarg (i val)
  528.   `(setf (narg-arg ,i) ,val))
  529.  
  530. ;;test of above
  531. ;(defun foo (&rest narg-rest-argument &aux (narg (length narg-rest-argument)))
  532. ;  (show  (narg-listify 3))
  533. ;  (show  (narg-listify -3))
  534. ;  (show  (narg-arg 2))
  535. ;  (narg-setarg 2 8)
  536. ;  (show (narg-listify 3)))
  537.  
  538. (defvar *reset-var* t)
  539.  
  540. (defvar *variable-initial-values* (make-hash-table)
  541.   "Hash table containing all Maxima defmvar variables and their initial
  542. values")
  543.  
  544. (defmacro defmvar (var &rest val-and-doc)
  545.   "If *reset-var* is true then loading or eval'ing will reset value, otherwise like defvar"
  546.   (cond ((> (length val-and-doc) 2)
  547.      (setq val-and-doc (list (car val-and-doc) (second val-and-doc)))))
  548.   `(progn
  549.     #+lispm (si::record-source-file-name ',var 'defmvar)
  550.     (unless (gethash ',var *variable-initial-values*)
  551.       (setf (gethash ',var *variable-initial-values*)
  552.         ,(first val-and-doc)))
  553.     (defvar ,var ,@ val-and-doc)
  554.     #+debug
  555.     (maybe-reset ',var ',(if val-and-doc (list (car val-and-doc))))))
  556.  
  557. (defun maybe-reset (var val-and-doc &aux val)
  558.   (cond (*reset-var*
  559.      (cond ((not(eq 'nil val-and-doc))
  560.         (cond ((not (equal (setq val (eval (car val-and-doc))) (symbol-value var)))
  561.                (format t "~%Replacing value of ~A" var)
  562.                (set var val))))
  563.            (t (cond ((boundp var)
  564.              (format t "~%Removing value of ~A" var)
  565.              (makunbound var))))))))
  566.  
  567. (defun $mkey (variable)
  568.   "($mkey '$demo)==>:demo"
  569.    (intern (string (string-left-trim "$" (string variable))) 'keyword))
  570.  
  571. ;;Problems with quote char / and #/ don't want to break editor or compiler.
  572. ;;Solution:
  573. ;; I replace all #/ by #\
  574. ;;II do the replacements indicated below under multiple query replace from buffer
  575. ;;III if a macro requires the actual string eg "//" use #.forward-slash-string for example.
  576. ;;have done this to suprv1,system,displa,nparse,displm.
  577.  
  578. ;;;some solutions for the backslash problem: in general try to avoid
  579. ;;using any quote character.  Use #\a for normal type characters.
  580. ;;to run in common (of course the numbers here need changing if not standard ascii):
  581.  
  582. (defvar double-quote-char (code-char 34.)) ;; #\")
  583. (defvar semi-colon-char (code-char 59.))   ;; #\;)
  584. (defvar back-slash-char (code-char 92.))   ;; #\\)
  585. (defvar forward-slash-char (code-char 47.)) ;; #\/)
  586. (defvar left-parentheses-char (code-char 40.))  ;(
  587. (defvar right-parentheses-char (code-char 41.)) ;)
  588. (defvar period-char (code-char 46.))            ;.
  589. (defvar vertical-stroke-char (code-char 124.))  ;|
  590. (defvar $forward-slash-symbol #-cl '$// #+cl '$/ )
  591. ;(defvar $colon-char (intern "$:"))
  592. ;(defvar $comma-char (intern "$,"))
  593.  
  594.  
  595. (defvar forward-slash-string (string forward-slash-char))
  596.  
  597. ;To handle the old narg syntax
  598.  
  599. ;(shadow '(arg listify setarg) 'maxima)
  600.  
  601. (defmacro arg (x)
  602.   `(narg1 ,x narg-rest-argument))
  603.  
  604. (defun narg1 (x l &aux tem)
  605.   (cond ((null x) (length l))
  606.     (T (setq tem (nthcdr (f1- x) l))
  607.        (cond ((null tem) (error "arg ~A beyond range ~A " x (length l)))
  608.          (t (car tem))))))
  609.  
  610. (defmacro listify (x)
  611.    `(listify1 ,x narg-rest-argument))
  612.  
  613. (defmacro setarg (i val)
  614.    `(setarg1 ,i ,val narg-rest-argument))
  615.  
  616. (defun setarg1 (i val l)
  617.   (setf (nth (f1- i)l) val) val)
  618.  
  619. (defun listify1 (n narg-rest-argument)
  620.   (cond ((minusp n) (copy-list (nleft (f- n) narg-rest-argument)) )
  621.     ((zerop n) nil)
  622.     (t (firstn n narg-rest-argument))))
  623.  
  624. ;(DEFVAR *LEXPR-ARGLIST*)
  625.  
  626. ;;to we need to bind *lexpr-arglist* to narg-rest-argument. 
  627. ;;its less efficient and SURELY no code refers to it. 
  628.  
  629. (Defmacro DEFMFUN (function &body  REST &aux .n.)
  630.   #+NIL (macsyma-defmfun-declarations function rest)
  631.   (cond ((and (car rest) (symbolp (car rest)))
  632.      ;;old maclisp narg syntax
  633.      (setq .n. (car rest))
  634.      (setf (car rest)
  635.            `(&rest narg-rest-argument &aux
  636.                (, .n. (length narg-rest-argument))
  637.                ;;(*lexpr-arglist*  narg-rest-argument) 
  638.                ))))
  639.   `(progn #-cl 'compile
  640.      #+lispm (si::record-source-file-name ',function 'defmfun)
  641.      (DEFUN ,FUNCTION . ,REST)))
  642.  
  643. ;;sample usage
  644. ;;(defmfun foo a (show a )(show (listify a)) (show (arg 3)))
  645.  
  646.  
  647. #+obsolete
  648. (defun string-from-char-list (list &aux str)
  649.   (let #+lispm ((default-cons-area working-storage-area)) #-lispm nil
  650.     (setq str  (make-string (length list)))
  651.     (sloop for v in  list
  652.       for i from 0
  653.       do (cond 
  654.            ((characterp v)
  655.         (setf (aref str i) v))
  656.            ((numberp v) (setf (aref str i) (code-char v)))
  657.            ((symbolp v)   (setf (aref str i) (aref (symbol-name v) 0)))
  658.            (t (error "unknown type")))
  659.       finally (return str))))
  660.   
  661.  
  662. (defvar *big-chunk-size*  120)
  663. (defvar *tentochunksize* (expt 10 *big-chunk-size*))
  664.  
  665. (defun exploden (symb &aux string)
  666.   (cond ((symbolp symb)(setq string (symbol-name symb)))
  667.         ((floatp symb)
  668.      (let ((a (abs symb)))
  669.        (cond ((or (eql a 0.0)
  670.               (and (>= a .001)
  671.                (<= a 10000000.0)))
  672.             (setq string (format nil "~vf" (+ 1 $fpprec) symb)))
  673.          (t (setq string (format nil "~ve" (+ 4 $fpprec) symb)))))
  674.      (setq string (string-left-trim " " string))
  675.      )
  676.     #+(and gcl (not gmp))
  677.     ((bignump symb)
  678.      (let* ((big symb)
  679.         ans rem tem
  680.            (chunks
  681.         (sloop 
  682.          do (multiple-value-setq (big rem)
  683.                      (floor big *tentochunksize*))
  684.          collect rem 
  685.          while (not (eql 0 big))
  686.          )))
  687.        (setq chunks (nreverse chunks))
  688.        (setq ans (list-string  (format nil "~d" (car chunks))))
  689.        (sloop for v in (cdr chunks)
  690.           do (setq tem (list-string (format nil "~d" v)))
  691.           (sloop for i below (-  *big-chunk-size* (length tem))
  692.              do (setq tem (cons #\0 tem)))
  693.           (setq ans (nconc ans tem)))
  694.        (return-from exploden ans)))
  695.     (t (setq string (format nil "~A" symb))))
  696.   (assert (stringp string))
  697.   (list-string string)
  698.   )
  699.  
  700.  
  701.  
  702. (defun explodec (symb &aux tem sstring)
  703.   (setq sstring (format nil "~a" symb))
  704.   ;(setq sstring (coerce symb 'string))
  705.   (sloop for v on (setq tem (list-string sstring))
  706.     do (setf (car v)(intern (string (car v)))))
  707.   tem)
  708.  
  709. (defvar *string-for-implode* (make-array 20 :fill-pointer 0 :adjustable t :element-type ' #. (array-element-type "ab")))
  710. (defun implode (lis) (implode1 lis nil))
  711.  
  712. (defun implode1 (lis upcase &aux (ar *string-for-implode*) (leng 0))
  713.   (declare (type string ar) (fixnum leng))
  714.   (or (> (array-total-size ar) (setq leng (length lis)))
  715.       (adjust-array ar (+ leng 20)))
  716.   (setf (fill-pointer ar) leng)
  717.   (sloop for v in lis
  718.      for i below leng
  719.      do
  720.      (cond ((typep v 'character))
  721.            ((symbolp v) (setq v (aref (symbol-name v) 0)))
  722.            ((numberp v) (setq v (code-char v))))
  723.      (setf (aref ar i) (if upcase (char-upcase v) v)))
  724.   (intern ar))
  725.  
  726. (defun bothcase-implode (lis  &aux tem )
  727.   (cond ((not (eql (car lis) #\$))
  728.      (return-from bothcase-implode (implode1 lis nil))))
  729.   (multiple-value-bind
  730.    (sym there)
  731.    (implode1 lis nil)
  732.    (cond (there (if (setq tem (get sym 'upcase)) tem sym))
  733.      (t
  734.       ;; if all upper case lets not bother interning...
  735.       (sloop for v in lis with haslower
  736.          when (not (eql (char-upcase v) v))
  737.          do (setq haslower t) (loop-finish)
  738.          finally (or haslower (return-from bothcase-implode sym)))
  739.       (multiple-value-bind
  740.        (symup there)
  741.        (implode1 lis t)
  742.        (cond ((and there (or
  743.                   ;; not single symbols
  744.                   (cddr lis)
  745.                   (fboundp symup) (symbol-plist symup)))
  746.                
  747.           (setf (get sym 'upcase) symup)
  748.           symup)
  749.          (t (or there (unintern symup))
  750.             sym)))))))
  751.  
  752.  
  753. (defun list-string (strin &aux tem)
  754.   (setq tem (make-list (length (the string  strin))))
  755.   (sloop for v on tem
  756.     for i from 0
  757.     do (setf (car v) (aref strin i)))
  758.   tem)
  759.  
  760. (defun explode (symb &aux tem sstring)
  761.   (setq sstring (format nil "~s" symb))
  762.   (sloop for v on (setq tem (list-string sstring))
  763.     do (setf (car v)(intern (string (car v)))))
  764.   tem)
  765.  
  766.  
  767. #-symbolics
  768. (defun getcharn (symb i &aux strin)
  769.   (setq strin (string symb))
  770.   (cond ((and (<= i (length strin)) (> i 0))
  771.      (aref strin (f- i 1)))
  772.     (t (MAXIMA-ERROR "out of bounds"))))
  773.  
  774. ;;Isn't this wonderful:
  775. ;;$B2 is in the CL-MAXIMA package
  776. ;;$B2 has SCL::PRINT-NAME property #"zzzzzz"
  777.  
  778. #-lispm
  779. (defmacro zl-string (x) x)
  780.  
  781. #+lispm
  782. (defun zl-string (cl-string &aux str)
  783.   #+zlch cl-string
  784.   #-zlch
  785.   (cond ((and (arrayp cl-string)
  786.           (not (fixnump (aref cl-string 0))))
  787.      (setq str (global:make-array  (length cl-string) :type 'global:art-string))
  788.      (sloop for i below (length cl-string)
  789.            do (setf (aref str i) (char-int (aref cl-string i))))
  790.      str)
  791.     (t cl-string)))
  792.  
  793. (defun zl-char (char)
  794.   #+zlch char
  795.   #-zlch (if (numberp char) char (char-int char)))
  796.  
  797. #+symbolics ;;because of SCL bug
  798. (defun getcharn (symb i &aux zl-strin)
  799.   (setq zl-strin (global:string symb))
  800.   (cond ((and (<= i (length zl-strin)) (> i 0))
  801.      #-zlch (code-char(aref zl-strin (f- i 1)))
  802.      #+zlch (aref zl-strin (f- i 1))
  803.      )
  804.     (t (MAXIMA-ERROR "out of bounds"))))
  805.  
  806. (defun getchar (symb i &aux strin)
  807.   (setq strin (string symb))
  808.   (cond ((and (<= i (length strin)) (> i 0))
  809.      (intern (string (aref strin (f- i 1)))))
  810.     (t nil)))
  811.  
  812. (defun ascii (n)
  813.   (intern (string n)))
  814.  
  815. (defun maknam (lis)
  816.   (sloop for v in lis
  817.     when (symbolp v)
  818.     collecting (getcharn v 1) into tem
  819.     else
  820.     when (characterp v)
  821.     collecting v into tem
  822.     else do (MAXIMA-ERROR "bad entry")
  823.     finally 
  824.     (return (make-symbol (coerce tem 'string)))))
  825.  
  826. ;;for those window labels etc. that are wrong type.
  827.  
  828. (defun flatc (sym)
  829.   (length (explodec sym)))
  830.  
  831. (defun flatsize (sym &aux (*print-circle* t))
  832.   (length (exploden sym)))
  833.  
  834. (defmacro safe-zerop (x)
  835.   (cond((symbolp x)`(and (numberp ,x) (zerop ,x)))
  836.        (t `(let ((.x. ,x))
  837.         (and (numberp .x.) (zerop .x.))))))
  838.  
  839. (defmacro signp (sym x)
  840.   (cond ((atom x)
  841.      (let ((test
  842.           (case sym
  843.         (e `(zerop ,x))
  844.         (l `(< ,x 0))    
  845.         (le `(<= ,x 0))
  846.         (g `(> ,x 0))
  847.         (ge `(>= ,x 0))
  848.         (n `(not (zerop ,x))))))
  849.        `(and (numberp ,x) ,test)))
  850.     (t `(let ((.x. ,x))
  851.          (signp ,sym .x.)))))
  852.  
  853.  
  854. (defmacro comment (&rest a) a ''comment)
  855.  
  856. (defun tyo (char &optional( stream *standard-output*))
  857.   (write-char char stream))
  858.  
  859. (defun tyi (&optional (stream *standard-input*) eof-option )
  860.    (cond (eof-option                
  861.         (read-char stream nil  eof-option))
  862.      (t(read-char stream nil nil))))
  863.  
  864.  
  865.  
  866. (DEFUN TYIPEEK (&OPTIONAL PEEK-TYPE &REST READ-ARGS)
  867.   (cond (read-args
  868.      (peek-char peek-type (car read-args)))
  869.     (t (peek-char peek-type))))
  870.  
  871. ;I don't think these are terribly useful so why use them.
  872.  
  873. #-ti
  874. (progn 'compile
  875. (defmacro *expr (&rest x) x nil)
  876. (defmacro *lexpr (&rest x) x nil)
  877. (defmacro *fexpr (&rest x) x nil)
  878. )
  879.  
  880. (defmacro local-declare (dcls &body body)
  881.   dcls ;ignore
  882.       `(progn
  883. ;        (declare ,@ dcls)
  884.         ,@ body))
  885.  
  886.  
  887. ;(defmacro symbol-array (sym)
  888. ;  `(symbol-function ',sym))
  889.   
  890. (defmacro arraycall (ign array &rest dims) ign
  891.   `(aref ,array . ,dims))
  892.  
  893.  
  894. ;(DEFMACRO-DISPLACE ARRAYCALL (IGNORE ARRAY &REST DIMS)
  895. ;  `(FUNCALL ,ARRAY . ,DIMS))
  896.  
  897. ;(defun readlist (lis)
  898. ;  (read-from-string   (coerce lis 'string)))
  899.  
  900. ;#-ti
  901. ;(defun make-equal-hash-table (&rest l)
  902. ;  (apply 'make-hash-table :test 'equal  l))
  903.  
  904. ;#-ti
  905. ;(defun array-length(x) (length x))
  906.  
  907. (defmacro copy-rest-arg (arg)
  908.   #+lispm `(copy-list ,arg)
  909.   #-lispm arg
  910.   )
  911.  
  912. (defvar ^W nil)
  913. (defvar ^R nil)
  914.  
  915. #+lispm
  916. (defun cursorpos (&rest args &aux (str *standard-output*) q1 q2 )
  917.   (cond ((null args)
  918.      (multiple-value-bind
  919.        (x y)
  920.          (send str :read-cursorpos ':character)
  921.        (cons y x)))
  922.     (t
  923.      (setq q1 (first args))(setq q2 (second args))
  924.      (cond ((or (null q1) (and (fixnump q1 ) (null q2)))
  925.         (multiple-value-bind (x y)(send str :read-cursorpos)
  926.              (send str :set-cursorpos (or q2 x) (or q1 y) :character) t))
  927.            ((and (fixnump q1) (fixnump q2))
  928.          (send str :set-cursorpos    q2 q1  :character) t)
  929.            (t (cond ((symbolp q1) (setf q1 (aref (symbol-name q1) 0)) t)
  930.             (t (error "bad first arg to cursorpos")))
  931.           (case (char-downcase q1)
  932.             (#\a  (send str :fresh-line) t)
  933.             (#\b (send str :tyo #\backspace) t)
  934.             (#\c (send str :clear-window) t)
  935.             (#\e (send str #+ti :clear-eof #-ti :clear-rest-of-window) t )
  936.             (#\f (send str :tyo #\space) t )
  937.             (#\k (send str :clear-char) t)
  938.             (#\l (send str #+ti :clear-eol #-ti :clear-rest-of-line) t)
  939.             (#\z (send str :home-down) t)
  940.             (#\x (send str :tyo #\space) t)
  941.             (#\t (send str :home-cursor) t)
  942.             (otherwise (error "unknown arg for this simple cursorpos"))))))))
  943.  
  944. ;;essentially no common lisp support for cursor pos:
  945. ;;should be adapted for a particular implementation.
  946. ;;the #+nocp (no cursorpos) flag could then be removed from *features*
  947. ;;and perhaps even smart-tty set to t.
  948. #-lispm 
  949. (defun cursorpos (&rest args &aux q1 q2 )
  950.   (cond ((null args) (error "cursorpos doesn't know position"))
  951.     (t
  952.      (setq q1 (first args))
  953.      (setq q2 (second args))
  954.      (cond ((or (null q1) (and (fixnump q1 ) (null q2)))
  955.         (error "can't set cursor pos"))
  956.            ((and (fixnump q1) (fixnump q2))
  957.         (error "can't set cursor pos") t)
  958.            (t (cond ((symbolp q1) (setf q1 (aref (symbol-name q1) 0)) t)
  959.             (t (error "bad first arg to cursorpos")))
  960.           (case (char-downcase q1)
  961.             (#\a  (fresh-line) t)
  962.             (#\b (error "cant backspace") t)
  963.             (#\c (error "cant clear window") t)
  964.             (#\e (error "can't clear rest of window") t )
  965.             (#\f (princ " ") t )
  966.             (#\k (error "can't clear-char") t)
  967.             (#\l (error "can't clear end of line") t)
  968.             (#\z (error "can't home-down") t)
  969.             (#\x (princ " ") t)
  970.             (#\t (error "can't home up") t)
  971.             (otherwise (error "unknown arg for this simple cursorpos"))))))))
  972.  
  973. ;;don't need for maxima.
  974. ;(defun change-cursorpos (str xdif ydif)
  975. ;  (multiple-value-bind (x y)
  976. ;      (send str :read-cursorpos :character)
  977. ;    (send str :set-cursorpos (f+ x xdif) (f+ y ydif))))
  978.  
  979. ;(deff cursorpos #'global:cursorpos)
  980.  
  981. #+lispm
  982. (progn
  983. (defvar format:*print-special* 'my-print)
  984. ;;this allows the signalling of errors using the "abc ~M " syntax
  985.  
  986. format:
  987. (DEFFORMAT M (:ONE-ARG) (ARG PARAMS)
  988.        (lisp:apply *print-special* arg *format-output* params))
  989.  
  990. (defun my-print (obj &optional (output *standard-output*) &rest params &aux
  991.              (*standard-output* output)$display2d)
  992.   (declare (special $display2d))
  993.   params ;ignore
  994.     (mgrind obj output))
  995. )
  996.  
  997.  
  998.  
  999. ;
  1000. ;(sloop for v in  (list  1 2.3 nil "hi" 'bye #'+ (list 1 2) (cons 1 2) (^ 2 40); (make-array 3) )
  1001. ;      collecting (cons v (maclisp-typep v)))
  1002. ;;on explorer June 19/85
  1003. ;((1 . FIXNUM) (2.3 . FLONUM)
  1004. ;              (NIL . SYMBOL)
  1005. ;              ("hi" . STRING)
  1006. ;              (BYE . SYMBOL)
  1007. ;              (#<DTP-FEF-POINTER PLUS 13552362> . COMPILED-FUNCTION)
  1008. ;              ((1 2) . LIST)
  1009. ;              ((1 . 2) . LIST)
  1010. ;              (1099511627776 . BIGNUM)
  1011. ;              (#<ART-Q-3 20302251> . ARRAY))
  1012. ;;on symbolics June /85
  1013. ;((1 . FIXNUM) (2.3 . FLONUM)
  1014. ;              (NIL . SYMBOL)
  1015. ;              ("hi" . ARRAY)
  1016. ;              (BYE . SYMBOL)
  1017. ;              (#<DTP-COMPILED-FUNCTION ZETALISP:PLUS 22312724> . COMPILED-FUNCTION)
  1018. ;              ((1 2) . LIST)
  1019. ;              ((1 . 2) . LIST)
  1020. ;              (1099511627776 . BIGNUM)
  1021. ;              (#<ART-Q-3 40203511> . ARRAY))
  1022. ;;The references after ; are to the original maclisp types as on tops-20 macsyma-maclisp version 302.
  1023. ;;((1 . FIXNUM) (2.3 . FLONUM) ;ok
  1024. ;;              (NIL . SYMBOL) ;ok
  1025. ;;              ("hi" . ARRAY) ;want symbol "hi" is a symbol whose value is "hi" and with property 'internal-string-marker t
  1026. ;;              (BYE . SYMBOL) ;ok
  1027. ;;              (#<DTP-COMPILED-FUNCTION ZETALISP:PLUS 22312724> . COMPILED-FUNCTION) ;want 'random
  1028. ;;              ((1 2) . LIST) ;ok 
  1029. ;;              ((1 . 2) . LIST) ;ok
  1030. ;;              (1099511627776 . NUMBER) ;want bignum
  1031. ;;)
  1032. ;;
  1033. ;;
  1034.  
  1035. ;(defvar *alpha-omega*  (sloop for i below 128 collecting (code-char  i)))
  1036.  
  1037. ;#+(or cl utexas)
  1038. ;compiler:
  1039. ;(progn 'compile
  1040. ;#+ti
  1041. ;compiler:
  1042. ;(defun mwarn (&rest args)
  1043. ;  (apply 'warn  nil args))
  1044. ;#-ti
  1045. ;(deff mwarn #'warn)
  1046. ;
  1047. ;(defun check-=-for-chars (form &aux warn)
  1048. ;  (sloop for v in '(maxima::char maxima::linechar maxima::getcharn maxima::getlabcharn)
  1049. ;    when (maxima::appears-in (cdr form) v)
  1050. ;    do (setq warn v))
  1051. ;  (sloop for v in (cdr form)
  1052. ;    when (and (symbolp v) (string-search "ch" v))
  1053. ;    do (setq warn v))
  1054. ;  (sloop for v in (cdr form)
  1055. ;    when (and  (atom v)(member v maxima::*alpha-omega*))
  1056. ;    do (setq warn (car (member v maxima::*alpha-omega*))))
  1057. ;  (cond (warn (mwarn nil "Is = or char= correct since ~A appears in ~A" warn form))))
  1058. ;(putprop 'maxima::= 'check-=-for-chars 'style-checker)
  1059. ;)
  1060.  
  1061. ;(defun trivial-obsolete-warn (form)
  1062. ;  (warn nil "~A has a trivial definition and is obsolete" (car form)))
  1063. ;compiler:
  1064. ;(putprop 'cl-maxima::listen 'trivial-obsolete-warn 'compiler:style-checker)
  1065. ;
  1066. ;(load "cl-maxima-source:maxima;compile-warn")
  1067.  
  1068.  
  1069.  
  1070.  
  1071. ;;;the ti gcd in microcode was broken earlier, and we needed this,
  1072. ;;;but it is now ok and this can be removed.
  1073.  
  1074. ;#+ti  ;;remove this after all recompiles
  1075. ;(cond ((not (fboundp 'gcd))(deff gcd #'global:gcd))) 
  1076. ;(progn 'compile 
  1077.  
  1078. ;(defun gcd (m n)
  1079. ;  (setq m (abs m) n (abs n))
  1080. ;  (cond ((< n m)nil)
  1081. ;    (t (rotatef m n)))
  1082. ;  (cond ((zerop n) m)
  1083. ;    ((fixnump n)
  1084. ;     (setq m (mod m n))
  1085. ;     (cond ((zerop m) n)
  1086. ;           (t (bin-gcd m n))))
  1087. ;    (t (gcd  n (mod m n)))))
  1088.  
  1089.  
  1090. ;;;(bin-gcd 0 3) breaks but it should never be called!!
  1091. ;(defun bin-gcd (u v &aux (k 0)u2 v2 t2 tt)
  1092. ;  (sloop 
  1093. ;    do (setq u2 (ash u -1))
  1094. ;    when (not (eql (ash u2 1) u))
  1095. ;      do (return k)
  1096. ;    do (setq v2 (ash v -1))
  1097. ;    when (not (eql (ash v2 1) v))
  1098. ;      do (return k)
  1099. ;       do (setq u u2 v v2 k (f1+ k)))
  1100. ;  (prog ()
  1101. ;     B2
  1102. ;    (cond ((oddp u) (setq tt (f- v)))
  1103. ;          (t(setq tt (ash u -1))))
  1104. ;     B3B4
  1105. ;        (sloop  do (setq t2 (ash tt -1))
  1106. ;           when  (eql (ash t2 1) tt)
  1107. ;               do (setq tt t2)
  1108. ;          else do (return nil))
  1109. ;    (cond ((> tt 0) (setq u tt))
  1110. ;          (t (setq v (f- tt))))
  1111. ;    (setq tt (f- u v))
  1112. ;    (cond ((zerop tt)(return (ash u k)))
  1113. ;          (t (go b3b4)))))
  1114.  
  1115. ;;(defun gcd (a b)
  1116. ;;  (setq a (abs a) b (abs b))
  1117. ;;  (cond ((< a b)(rotatef a b)))
  1118. ;;  (cond ((zerop b) a)
  1119. ;;    (t
  1120. ;;     (gcd1 a b))))
  1121.  
  1122. ;;(defun gcd1 (a b &aux tem )
  1123. ;;  (setq tem (mod a b))
  1124. ;;  (cond ((zerop tem) b)
  1125. ;;    (t (gcd1 b tem ))))
  1126. ;)
  1127.  
  1128.  
  1129.  
  1130. (defvar *all-arrays* nil)
  1131. (defun function-array-p (sym)
  1132.   ;(push sym *all-arrays*)
  1133.   (arrayp (symbol-array sym)))
  1134.  
  1135.  
  1136. ;; no generic way of knowing args numbers..
  1137. (defmacro MARGCHK (FN ARGS) fn args ())
  1138.  
  1139. (defun $timedate ()
  1140.   (system "date"))
  1141.  
  1142.  
  1143. ;;Some systems make everything functionp including macros:
  1144. #+shadow-functionp
  1145. (defun functionp (x)
  1146.    (cond ((symbolp x)
  1147.           (and (not (macro-function x))
  1148.            (fboundp x) t))
  1149.      ((lisp::functionp x))))
  1150.       
  1151. (defun file-to-string (x)
  1152.   (with-open-file
  1153.    (st x)
  1154.    (let* ((n (file-length st))
  1155.       (ar (make-array n :element-type '#.(array-element-type "a")))
  1156.       )
  1157.      (declare (type (array #.(array-element-type "a")) ar))
  1158.      (sloop for i below n 
  1159.         for tem = (read-char st nil)
  1160.         do 
  1161.         (if tem    (setf (aref ar i) tem)))
  1162.      ar)))
  1163.      
  1164.           
  1165.           
  1166.  
  1167.